package com.chenfan.mcn.extension;

import com.chenfan.common.vo.Response;
import com.chenfan.mcn.exception.McnErrorCode;
import org.apache.commons.lang.StringUtils;

/**
 * @author lizhejin
 */
public class Res<T> extends Response<T> {

    public Res(int code, String message) {
        this.setCode(code);
        this.setMessage(message);
    }

    public Res(McnErrorCode errorCode) {
        this.setCode(errorCode.code());
        this.setMessage(errorCode.message());
    }

    public Res(McnErrorCode errorCode, T data) {
        this.setCode(errorCode.code());
        this.setMessage(errorCode.message());
        this.setObj(data);
    }

    public static <T> Res<T> ok() {
        return new Res<>(McnErrorCode.SUCCESS);
    }

    public static <T> Res<T> ok(T data) {
        return new Res<>(McnErrorCode.SUCCESS, data);
    }

    public static <T> Res<T> failed() {
        return new Res<>(McnErrorCode.FAILED);
    }

    public static <T> Res<T> failed(T data) {
        return new Res<>(McnErrorCode.FAILED, data);
    }

    public static <T> Res<T> failed(String msg) {
        return new Res<>(McnErrorCode.FAILED.code(), msg);
    }

    public static <T> Res<T> failed(McnErrorCode errorCode) {
        return new Res<>(errorCode);
    }

    public static <T> Res<T> failed(McnErrorCode errorCode, String message) {
        return new Res<>(errorCode.code(), StringUtils.defaultIfBlank(message, errorCode.message()));
    }
}
